package com.xydpeng.rpc.protocol;

import io.netty.util.Recycler;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author xydpeng
 * @version v1.0
 * @date: 2023/4/13
 * @description:
 *
 *   协议头固定长度18 byte
 *     +---------------------------------------------------------------+
 *     | 魔数 2byte | 协议版本号 1byte | 序列化算法 1byte | 报文类型 1byte  |
 *     +---------------------------------------------------------------+
 *     | 状态 1byte |        消息 ID 8byte     |      数据长度 4byte     |
 *     +---------------------------------------------------------------+
 *
 */
@Setter
@Getter
public class MsgHeader implements Serializable {
    private Recycler.Handle<MsgHeader> handle;


    public static final int PROTOCOL_HEADER_LEN = 18; //18 byte
    public static final short MAGIC = 0x7afa; //18 byte

    private short magic; // 魔数
    private byte version; // 协议版本号
    private byte serialization; // 序列化算法
    private byte msgType; // 报文类型
    private byte status; // 状态
    private long requestId; // 消息 ID
    private int msgLen; // 数据长度


    public MsgHeader(Recycler.Handle<MsgHeader> handle) {
        this.handle = handle;
    }

    public void recycle(){
        handle.recycle(this);
    }
}
